package com.Khan.SocialRescueSys.controller;

import com.Khan.SocialRescueSys.pojo.Message;
import com.Khan.SocialRescueSys.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Khan Liu
 * @date 2022/4/5 13:18
 */
@Controller
public class NewsController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/news")
    public String toNews(@RequestParam(required = false, defaultValue = "1", value = "pagenum") int pagenum, Model model) {
        PageHelper.startPage(pagenum, 6);
        List<Message> allNews = messageService.getNews();

        PageInfo pageInfo = new PageInfo(allNews);
        model.addAttribute("pageInfo", pageInfo);
        return "news";
    }

    @GetMapping("/news-detail/{id}")
    public String toNewsDetail(@PathVariable("id") Long id, Model model) {
        Message newsDetail = messageService.getNewsDetail(id);
        model.addAttribute("news", newsDetail);
        return "news-detail";
    }
}
